package com.qf.bigwork.view;

import android.app.NotificationManager;
import android.content.Context;
import android.os.Build;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.qf.bigwork.R;
import com.qf.bigwork.fragment.BeijingFragment;
import com.qf.bigwork.fragment.CommunityFragment;
import com.qf.bigwork.fragment.MessageFragment;
import com.qf.bigwork.fragment.MyselfFragment;
import com.qf.bigwork.fragment.VideoFragment;
//主页
public class MainActivity extends AppCompatActivity {

    private Fragment[] frag;
    private int mIndex;
    private NotificationManager manager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initWindow();//状态栏透明
        initView();//控件实例化
        initFragment();//添加碎片
    }
    private void initWindow() {
        //使状态栏透明
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
    }
    private void initView() {
        manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    }
    private void initFragment() {
        //资讯
        MessageFragment messageFragment = new MessageFragment();
        //视频
        VideoFragment videoFragment = new VideoFragment();
        //北京
        BeijingFragment beijingFragment =new BeijingFragment();

        //社区
        CommunityFragment communityFragment = new CommunityFragment();
        //我的
        MyselfFragment mySelfFragment = new MyselfFragment();
        //添加到数组
        frag = new Fragment[]{messageFragment, videoFragment,beijingFragment, communityFragment, mySelfFragment};
        //获取碎片的事务对象，开启事务
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        //添加碎片，提交 添加发现页
        ft.add(R.id.zhu_fl, messageFragment).commit();
        //默认设置为第0个
        setIndexSelected(0);
    }

    //切换fragment方法
    private void setIndexSelected(int index) {
        if (mIndex == index) {
            return;
        }
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction ft = fragmentManager.beginTransaction();
        //隐藏
        ft.hide(frag[mIndex]);
        //判断是否添加
        if (!frag[index].isAdded()) {
            ft.add(R.id.zhu_fl, frag[index]).show(frag[index]);
        } else {
            ft.show(frag[index]);
        }
        ft.commit();
        //再次赋值
        mIndex = index;
    }


    public void click(View view) {
        switch (view.getId()){
            case R.id.zhu_rb_zi:
                setIndexSelected(0);
                break;
            case R.id.zhu_rb_shi:
                setIndexSelected(1);
                break;
            case R.id.zhu_rb_bei:
                setIndexSelected(2);
                break;
            case R.id.zhu_rb_she:
                setIndexSelected(3);
                break;
            case R.id.zhu_rb_wo:
                setIndexSelected(4);
                break;
        }
    }
}
